package wiki.xsx.config;

/**
 * @Descript
 * @Author xsx
 * @Date 2017/11/16
 */
public enum TimeType {
    /**
     * Time unit representing sixty seconds
     */
    MINUTES {
        public long toSeconds(Integer d) { return x(d, C4/C3, MAX/(C4/C3)); }
    },

    /**
     * Time unit representing sixty minutes
     */
    HOURS {
        public long toSeconds(Integer d) { return x(d, C5/C3, MAX/(C5/C3)); }
    },

    /**
     * Time unit representing twenty four hours
     */
    DAYS {
        public long toSeconds(Integer d) { return x(d, C6/C3, MAX/(C6/C3)); }
    };

    // Handy constants for conversion methods
    static final long C0 = 1L;
    static final long C1 = C0 * 1000L;
    static final long C2 = C1 * 1000L;
    static final long C3 = C2 * 1000L;
    static final long C4 = C3 * 60L;
    static final long C5 = C4 * 60L;
    static final long C6 = C5 * 24L;

    static final long MAX = Long.MAX_VALUE;

    /**
     * Scale d by m, checking for overflow.
     * This has a short name to make above code more readable.
     */
    private static long x(long d, long m, long over) {
        if (d >  over) return Long.MAX_VALUE;
        if (d < -over) return Long.MIN_VALUE;
        return d * m;
    }

    public long toSeconds(Integer duration) {
        throw new AbstractMethodError();
    }
}
